package cn.com.gridinfo.sooc.core.expand.callname.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.expand.callname.activity.AddStudentActivity;
import cn.com.gridinfo.sooc.core.expand.callname.bean.Student;

/**
 * 学生名单界面列表适配器
 *
 * @author yingchunli
 */
public class StudentMenuAdapter extends UltimateViewAdapter<StudentMenuAdapter.ViewHolder> {

    public List<Student> studentList;
    Context context;

    public StudentMenuAdapter(List<Student> studentList, Context context) {
        this.studentList = studentList;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.expand_callname_studentmenu_item, parent, false);
        ViewHolder holder = new ViewHolder(view, true);
        view.setOnLongClickListener(holder);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= studentList.size() : position < studentList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            Student student = studentList.get(position);
            holder.xm.setText(student.getXm()+"（"+student.getXh()+"）");
            holder.bj.setText(student.getBj());
        }
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void OnItemClick(View view, Student student,int position);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    //加载更多数据
    public void loadmore(List<Student> list) {
        studentList.addAll(list);
        notifyDataSetChanged();
    }

    //下拉刷新数据
    public void refresh(List<Student> list) {
        studentList.clear();
        studentList.addAll(list);
        notifyDataSetChanged();
    }

    //删除学生
    public void delete(int positon){
        studentList.remove(positon);
        notifyDataSetChanged();
    }

    @Override
    public int getAdapterItemCount() {
        return studentList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder implements  View.OnLongClickListener {
        TextView xm; //姓名
        TextView bj;//班级

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                xm = (TextView) itemView.findViewById(R.id.xm_studentmenu_item);
                bj = (TextView) itemView.findViewById(R.id.bj_studentmenu_item);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            Log.d("长按");
            mOnItemClickLitener.OnItemClick(v,studentList.get(getAdapterPosition()),getAdapterPosition());
            return false;
        }
    }
}
